﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace JHelp
{
    /// <summary>
    /// 缓存
    /// </summary>
    public class WebCache:IJCache
    {
        private static  System.Web.Caching.Cache _currentCache;

        public static System.Web.Caching.Cache CurrentCache
        {
            get
            {
                if (_currentCache == null)
                   _currentCache=System.Web.HttpRuntime.Cache;
                return _currentCache;
            }
           
        }
        /// <summary>
        ///获取单个缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        static object GetCache(string key)
        {
            return CurrentCache.Get(key);
        }
        /// <summary>
        /// 增加缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="obj">值</param>
        static void AddCache(string key, object obj)
        {
             CurrentCache.Insert(key, obj);
        }

        /// <summary>
        /// 增加缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="obj">值</param>
        /// <param name="TimeOut">过期时间间隔</param>
        static void AddCache(string key, object obj, TimeSpan TimeOut) {
            CurrentCache.Insert(key, obj, null,System.Web.Caching.Cache.NoAbsoluteExpiration, TimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }
        /// <summary>
        /// 增加缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="obj">值</param>
        /// <param name="absoluteExpiretion">绝对过期时间</param>
        /// <param name="TimeOut"></param>
        static void AddCache(string key, object obj, DateTime absoluteExpiretion, TimeSpan TimeOut)
        {
            CurrentCache.Insert(key, obj, null, absoluteExpiretion, TimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);

        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>返回布尔类型</returns>
        static bool RemoveCache(string key) {
            if (CurrentCache.Remove(key) == null)
                return false;
            return true;
        }
    }
}
